package com.qiandw.service;

import com.qiandw.entity.SysUser;
import com.qiandw.repo.SysUserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 关于 UserDetailService 实现
 * @author coxx
 */
@Service("UserDetailServiceImpl")
public class UserDetailServiceImpl implements UserDetailsService {

    private final SysUserRepo sysUserRepo;

    @Autowired
    public UserDetailServiceImpl(SysUserRepo sysUserRepo) {
        this.sysUserRepo = sysUserRepo;
    }

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //通过 username 获取 user 信息
        SysUser user = sysUserRepo.findSysUserByUsername(userName);
        if (user == null) {
            throw new UsernameNotFoundException("UserName " + userName + " not found");
        }
        return user.convertSecurityUser();
    }
}
